package de.bazinga.rotw.statistics;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Collections;
import java.util.Vector;

import com.jme.image.Texture;
import com.jme.math.Vector2f;
import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.Spatial;
import com.jme.scene.TexCoords;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.BlendState;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;
import com.jmex.game.state.BasicGameState;
import com.jmex.game.state.GameState;
import com.jmex.game.state.GameStateManager;

import de.bazinga.rotw.game.Game;
import de.bazinga.rotw.game.RotwGameState;

public class StatisticState extends BasicGameState {
	private Quad stat = null;
	
	private TextureState mapTexture;
	private Texture texture;
	
	private long lastUpdate = 0;
	private long updateLag = 1000;
	
	private Vector<String> scores = new Vector<String>();

	public StatisticState() {
		super("statistics");
		
		init();
	}
	
	
	/**
	 * initializes the GameState
	 */
	private void init() {
		stat = new Quad("stat",
				Game.getGame().getDisplay().getWidth() - 100,
				Game.getGame().getDisplay().getHeight() - 100);
		
		
		//Texture
		mapTexture = Game.getGame().getDisplay().getRenderer().createTextureState();
		texture = TextureManager.loadTexture(getBufferedImage(),
				Texture.MinificationFilter.BilinearNearestMipMap , 
				Texture.MagnificationFilter.Bilinear,
				false);
		
		mapTexture.setTexture(texture);
		mapTexture.setEnabled(true);

		
		stat.setRenderQueueMode(Renderer.QUEUE_ORTHO);
		
		stat.setLocalTranslation(new Vector3f(
				Game.getGame().getDisplay().getWidth()/2.0f,
				Game.getGame().getDisplay().getHeight()/2.0f,
				1));
		
		stat.setRenderState(mapTexture);
		
		
		BlendState bs = Game.getGame().getDisplay().getRenderer().createBlendState();
		bs.setBlendEnabled(true);
		bs.setSourceFunction(BlendState.SourceFunction.SourceAlpha);
		bs.setDestinationFunction(BlendState.DestinationFunction.OneMinusSourceAlpha);
		bs.setTestEnabled(true);
		bs.setTestFunction(BlendState.TestFunction.GreaterThan);
		
		stat.setRenderState(bs);
		
		stat.setLightCombineMode(Spatial.LightCombineMode.Off);
		stat.updateRenderState();
		
		getRootNode().attachChild(stat);
	}
	
	
	/**
	 * Updates the rootNode.
	 * 
	 * @see GameState#update(float)
	 */
	public void update(float tpf) {
		
		if( (System.currentTimeMillis() - lastUpdate) > updateLag) {
			//Texture
			mapTexture = Game.getGame().getDisplay().getRenderer().createTextureState();
			texture = TextureManager.loadTexture(getBufferedImage(),
					Texture.MinificationFilter.BilinearNearestMipMap , 
					Texture.MagnificationFilter.Bilinear,
					false);
			mapTexture.setTexture(texture);
			mapTexture.setEnabled(true);
			stat.setTextureCoords(TexCoords.makeNew(
					new Vector2f[]{
							new Vector2f(0,0),
							new Vector2f(0,1),
							new Vector2f(1,1), 
							new Vector2f(1,0)}));
			stat.setRenderState(mapTexture);
			stat.updateRenderState();
			
			lastUpdate = System.currentTimeMillis();
		}
	}
	


	/**
	 * Draws the rootNode.
	 * 
	 * @see GameState#render(float)
	 */
	public void render(float tpf) {
		super.render(tpf);
	}
	
	
	
	public BufferedImage getBufferedImage() {
		BufferedImage img = new BufferedImage(512,512,BufferedImage.TYPE_INT_ARGB);
		
		Graphics2D g = img.createGraphics();
		
		g.setColor(new Color(150,150, 150, 100));
		g.fillRect(0, 0, img.getWidth(), img.getHeight());
		
		g.setColor(Color.black);
		g.drawRect(0, 0, img.getWidth() - 1, img.getHeight() - 1);
		
		g.setColor(Color.black);
		
		g.setFont(new Font("Arial", Font.BOLD, 30));
		g.drawString("Statistik", 50, 50);
		
		

		g.setFont(new Font("Arial", Font.BOLD, 20));
		g.drawString("Spieler", 70, 100);
		g.drawString("Score", 400, 100);
		
		g.drawLine(0, 105, Game.getGame().getDisplay().getWidth(), 105);
		
		
		// Sort
		
		Collections.sort(scores, new ScoreComparator());
		
		Vector<String[]> teamOne = new Vector<String[]>();
		Vector<String[]> teamTwo = new Vector<String[]>();
		Vector<String[]> allPlayer = new Vector<String[]>();
		
		for(String p:scores) {
			String[] data = p.split("__::__");
			if(data.length == 3) {
				if(data[2].equals("1")) {
					teamOne.add(data);
				}else{
					teamTwo.add(data);
				}
				allPlayer.add(data);
			}
		}

		if(((RotwGameState)GameStateManager.getInstance().getChild("gameState")).useTeams()) {
			g.drawString("Team 1", 50, 150);
			g.drawString(String.valueOf(RotwGameState.teamOneScore), 350, 150);
			
			int c=1;
			for(String[] d:teamOne) {
				g.drawString(d[1], 70, 150 + c*50);
				g.drawString(d[0], 400, 150 + c*50);
				c++;
			}
			
			g.drawString("Team 2", 50, 150 + c*50);
			g.drawString(String.valueOf(RotwGameState.teamTwoScore), 350,  150 + c*50);
			c++;
			
			for(String[] d:teamTwo) {
				g.drawString(d[1], 70, 150 + c*50);
				g.drawString(d[0], 400, 150 + c*50);
				c++;
			}
		}else{
			// No Teams!
			int c=1;
			for(String[] d:allPlayer) {
				g.drawString(d[1], 50, 100 + c*50);
				g.drawString(d[0], 400, 100 + c*50);
				c++;
			}
		}
		
		return img;
	}
	
	public Vector<String> getScores() {
		return scores;
	}


	public void setScores(Vector<String> scores) {
		this.scores = scores;
	}
}
